class Login{
	constructor(){
	  //给登录按钮设定点击事件
	  this.$('.over').addEventListener('click', this.islogin);
	  //console.log(location.search.split('='));
	 let search= location.search;
	 //console.log(search);
	 if(search){
	   this.url=search.split('=')[1]
	 }
	}
	islogin=()=>{
	  let form=document.forms[0].elements;
	  //console.log(form);
	  //获取账号和密码的值
	  let username=form.uname.value.trim();
	  let password=form.password.value.trim();
	  //console.log(username,password);
	  //非空验证
	  if(!username || ! password) throw new Error('账号或密码错误');
  
	  //发送axios请求
	  let param=`username=${username}&password=${password}`;
	 axios.post('http://localhost:8888/users/login', param,{
	  headers: {
		'Content-Type': 'application/x-www-form-urlencoded'
	  }
	  }).then(res=>{
		//console.log(res);
		//判断登录信息  
		if (res.data.code==1 && res.status==200){
		  localStorage.setItem('token',res.data.token);
		  localStorage.setItem('user_id',res.data.user.id);
		   
			location.href = this.url;
		   location.assign('./product.html')
		}
  
	  })
	}
	$(ele){
	  let res=document.querySelectorAll(ele);
	  return res.length==1?  res[0]:res;
   }
  }
  new Login;